package com.tao.controller;

import com.tao.mapper.BannerMapper;
import com.tao.pojo.entity.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
public class BannerController {

    @Value("${dirPath}")
    private String dirPath;

    @Autowired
    BannerMapper bannerMapper;

    @RequestMapping("/banner/select")
    public List<Banner> select() {
        return bannerMapper.select();
    }

    @RequestMapping("/banner/delete")
    public void delete(int id) {
        String url = bannerMapper.selectUrlById(id);
        new File(dirPath+url).delete();
        bannerMapper.delete(id);
    }

    @RequestMapping("/banner/insert")
    public void insert(String url) {
        bannerMapper.insert(url);
    }

    @RequestMapping("/banner/update")
    public List<Banner> update(@RequestBody Banner banner) {
        System.out.println("banner = " + banner);
        bannerMapper.update(banner);
        return bannerMapper.select();
    }
}
